package main

import (
	"fmt"
	"gitee.com/hlwqds/rabitmqTest/config"
	"gitee.com/hlwqds/rabitmqTest/log"
	"github.com/streadway/amqp"
)

func main(){
	url := config.GenURLFromConfig()
	conn, err := amqp.Dial(url)
	defer conn.Close()
	log.FailOnError(err, "amqp.Dial error")

	ch, err := conn.Channel()
	log.FailOnError(err, "conn.Channel error")
	defer ch.Close()

	var logExName = "amq.rabbitmq.log"

	errorsQueue, err := ch.QueueDeclare(
		"",
		false,
		true,
		false,
		false,
		nil)
	log.FailOnError(err, "ch.QueueDeclare error")
	warningsQueue, err := ch.QueueDeclare(
		"",
		false,
		true,
		false,
		false,
		nil)
	log.FailOnError(err, "ch.QueueDeclare error")
	infoQueue, err := ch.QueueDeclare(
		"",
		false,
		true,
		false,
		false,
		nil)
	log.FailOnError(err, "ch.QueueDeclare error")

	err = ch.QueueBind(
		errorsQueue.Name,
		"error",
		logExName,
		false,
		nil)
	log.FailOnError(err, "ch.QueueBind error")
	err = ch.QueueBind(
		warningsQueue.Name,
		"warning",
		logExName,
		false,
		nil)
	log.FailOnError(err, "ch.QueueBind error")
	err = ch.QueueBind(
		infoQueue.Name,
		"info",
		logExName,
		false,
		nil)
	log.FailOnError(err, "ch.QueueBind error")

	errCh, err := ch.Consume(
		errorsQueue.Name,
		"",
		false,
		true,
		false,
		false,
		nil,
		)
	log.FailOnError(err, "conn.QueueBind error")

	warningCh, err := ch.Consume(
		warningsQueue.Name,
		"",
		false,
		true,
		false,
		false,
		nil,
	)

	infoCh, err := ch.Consume(
		infoQueue.Name,
		"",
		false,
		true,
		false,
		false,
		nil,
	)

	/*
	系统预留的交换器是不能访问的
		ch.Publish(
			logExName,
			"info",
			false,
			false,
			amqp.Publishing{
				ContentType: "text/plain",
				Body: []byte("test"),
		})
	*/

	func(){
		var data amqp.Delivery
		var ifChannelExsit bool
		for{
			select {
			case data, ifChannelExsit = <-errCh:
			case data, ifChannelExsit = <-infoCh:
			case data, ifChannelExsit = <-warningCh:
			}
			if !ifChannelExsit {
				break
			}
			fmt.Println(data.Body)
			data.Ack(false)
		}
	}()

}
